trigger TEST_Event_afterInsert on Event (after insert) {

    //This is a test trigger to assure that Activity object fires trigger to create copy object, that also fires 
    //trigger to update copyObjectID into activity object and avoid recursive triggers is possible

        Map<String, Schema.SObjectType> gd = Schema.getGlobalDescribe(); 
        Map<String,String> keyPrefixMap = new Map<String,String>{};
        Set<String> keyPrefixSet = gd.keySet();
        for(String sObj : keyPrefixSet){
           Schema.DescribeSObjectResult r =  gd.get(sObj).getDescribe();
           String tempName = r.getName();
           String tempPrefix = r.getKeyPrefix();
           System.debug('Processing Object['+tempName + '] with Prefix ['+ tempPrefix+']');
           keyPrefixMap.put(tempPrefix,tempName);
        }


        String Contact_prefix = Schema.SObjectType.Contact.getKeyPrefix();
        String Contract_prefix = Schema.SObjectType.Contract.getKeyPrefix();
        String Campaign_prefix = Schema.SObjectType.Campaign.getKeyPrefix();
        String Account_prefix = Schema.SObjectType.Account.getKeyPrefix();
        String Opportunity_prefix = Schema.SObjectType.Opportunity.getKeyPrefix();
        String Asset_prefix = Schema.SObjectType.Asset.getKeyPrefix();
        String Case_prefix = Schema.SObjectType.Case.getKeyPrefix();
        String Solution_prefix = Schema.SObjectType.Solution.getKeyPrefix();
        String Product_prefix=Schema.SObjectType.Product2.getKeyPrefix();
        String Lead_prefix=Schema.SObjectType.Lead.getKeyPrefix();
        
        
        List<Event> events = new List<Event>();
        List<CopyEvent__c> copyEvents= new List<CopyEvent__c>();

        for (Event event : Trigger.new) {
            CopyEvent__c copyEvent= new CopyEvent__c();
            //copyEvent.Name=event.Description; 
            copyEvent.TEST_OriginalEvent_ID__c=event.id;
            copyEvent.Description__c = event.Description;
            copyEvent.Subject__c = event.Subject;
            
            String whatIdEvent=(String)event.WhatId;
            
            if(whatIdEvent!=null){
                String prefix=whatIdEvent.subString(0,3);
                copyEvent.MasterType__c=keyPrefixMap.get(prefix);
                System.Debug('## >>> _Event_afterInsert: Prefix '+prefix+' is '+keyPrefixMap.get(prefix)+'  <<< run by ' + UserInfo.getName());
                
                if(whatIdEvent.startsWith(Account_prefix)){
                    System.Debug('## >>> _Event_afterInsert: This is an account  <<< run by ' + UserInfo.getName());
                    copyEvent.Account__c = event.WhatId;
                }
            }
            
            String whoIdEvent=(String)event.WhoId;
            
            if(whoIdEvent!=null){    

                if(whoIdEvent.startsWith(Contact_prefix))
                {
                    copyEvent.Contact__c = event.WHOID ;
                }else if(whoIdEvent.startsWith(Lead_prefix))
                {
                    copyEvent.Lead__c = event.WHOID ;
                }
            }
            
            copyEvents.add(copyEvent);
        }
        
        insert copyEvents;

}